import api from '../../api'
import router from '../../router'
export default {
    namespaced: true, // 具名模块 this.$store.dispatch('home/SET_SHOP_LIST')
    // namespaced: false, // 具名模块 this.$store.dispatch('SET_SHOP_LIST')
    // 每次刷新页面都会重新 初始化仓库 
    state: {
        userData: null,
        token: '',
        expiresIn: null
    },
    mutations: {
        setUserData(state, data) {
            state.userData = data
        },
        setToken(state, data) {
            state.token = data
        },
        setExpiresIn(state, data) {
            state.expiresIn = data
        }
    },
    actions: {
        async LOGIN(context, { username, password }) {
            console.log({ username, password })
            const res = await api.login({ username, password })
            if (res.success) {
                context.commit('setUserData', res.data.userData)
                context.commit('setToken', res.data.token)
                context.commit('setExpiresIn', res.data.expiresIn)
                router.push('/home')
            } else {
                alert('失败了')
            }
        }
    }
}